package cn.wubo.one.table.ai;

import cn.wubo.one.table.ai.dto.ChatRequest;
import cn.wubo.one.table.ai.service.IOneTableAiService;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.http.MediaType;
import org.springframework.web.bind.annotation.*;
import reactor.core.publisher.Flux;

import javax.validation.Valid;
import java.util.Map;

@RequestMapping("/oneTableAi")
@RestController
public class OneTableAiController {

    private final IOneTableAiService oneTableAiService;

    public OneTableAiController(IOneTableAiService oneTableAiService) {
        this.oneTableAiService = oneTableAiService;
    }

    @GetMapping(value = "/buildRequirement", produces = MediaType.TEXT_EVENT_STREAM_VALUE)
    public Flux<String> buildRequirement(@RequestParam String text) {
        return oneTableAiService.buildRequirement(text).concatWith(Flux.just("<COMPLETE>"))
                .map(s -> "[" + s + "]")
                .doOnNext(System.out::println);
    }

    @GetMapping(value = "/buildClass", produces = MediaType.TEXT_EVENT_STREAM_VALUE)
    public Flux<String> buildClass(@RequestParam String text) {
        return oneTableAiService.buildClass(text).concatWith(Flux.just("<COMPLETE>"))
                .map(s -> "[" + s + "]")
                .doOnNext(System.out::println);
    }

    @PostMapping("/buildWeb")
    public Map<String, Object> buildWeb(@RequestBody @Valid ChatRequest chatRequest) {
        return oneTableAiService.buildWeb(chatRequest.getText());
    }
}
